﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using System.Text;

namespace Helpers
{
    /// <summary>
    /// Object的扩展
    /// </summary>
    public static class ObjectEx
    {
        /// <summary>
        /// 根据 FieldInfo获取一个指定的特性
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <param name="field"></param>
        /// <returns></returns>
        public static T GetCustomAttribute<T>(this object obj,FieldInfo field) where T:Attribute
        {
            if (obj != null)
            {
                return (T)Attribute.GetCustomAttribute(field, typeof(T));
            }
            return null;
        }
        /// <summary>
        /// 更加 FieldName获取一个指定的特性.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <param name="fieldName"></param>
        /// <returns></returns>
        public static T GetCustomAttribute<T>(this object obj, string fieldName) where T : Attribute
        {
            return GetCustomAttribute<T>(obj,obj.GetType().GetField(fieldName));
        }
    }
}
